{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2023 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# sudoku_sat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/examples/sudoku_sat.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/python/sudoku_sat.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "This model implements a sudoku solver.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.sat.python import cp_model\n",
    "\n",
    "\n",
    "def solve_sudoku():\n",
    "    \"\"\"Solves the sudoku problem with the CP-SAT solver.\"\"\"\n",
    "    # Create the model.\n",
    "    model = cp_model.CpModel()\n",
    "\n",
    "    cell_size = 3\n",
    "    line_size = cell_size**2\n",
    "    line = list(range(0, line_size))\n",
    "    cell = list(range(0, cell_size))\n",
    "\n",
    "    initial_grid = [\n",
    "        [0, 6, 0, 0, 5, 0, 0, 2, 0],\n",
    "        [0, 0, 0, 3, 0, 0, 0, 9, 0],\n",
    "        [7, 0, 0, 6, 0, 0, 0, 1, 0],\n",
    "        [0, 0, 6, 0, 3, 0, 4, 0, 0],\n",
    "        [0, 0, 4, 0, 7, 0, 1, 0, 0],\n",
    "        [0, 0, 5, 0, 9, 0, 8, 0, 0],\n",
    "        [0, 4, 0, 0, 0, 1, 0, 0, 6],\n",
    "        [0, 3, 0, 0, 0, 8, 0, 0, 0],\n",
    "        [0, 2, 0, 0, 4, 0, 0, 5, 0],\n",
    "    ]\n",
    "\n",
    "    grid = {}\n",
    "    for i in line:\n",
    "        for j in line:\n",
    "            grid[(i, j)] = model.NewIntVar(1, line_size, \"grid %i %i\" % (i, j))\n",
    "\n",
    "    # AllDifferent on rows.\n",
    "    for i in line:\n",
    "        model.AddAllDifferent(grid[(i, j)] for j in line)\n",
    "\n",
    "    # AllDifferent on columns.\n",
    "    for j in line:\n",
    "        model.AddAllDifferent(grid[(i, j)] for i in line)\n",
    "\n",
    "    # AllDifferent on cells.\n",
    "    for i in cell:\n",
    "        for j in cell:\n",
    "            one_cell = []\n",
    "            for di in cell:\n",
    "                for dj in cell:\n",
    "                    one_cell.append(grid[(i * cell_size + di, j * cell_size + dj)])\n",
    "\n",
    "            model.AddAllDifferent(one_cell)\n",
    "\n",
    "    # Initial values.\n",
    "    for i in line:\n",
    "        for j in line:\n",
    "            if initial_grid[i][j]:\n",
    "                model.Add(grid[(i, j)] == initial_grid[i][j])\n",
    "\n",
    "    # Solve and print out the solution.\n",
    "    solver = cp_model.CpSolver()\n",
    "    status = solver.Solve(model)\n",
    "    if status == cp_model.OPTIMAL:\n",
    "        for i in line:\n",
    "            print([int(solver.Value(grid[(i, j)])) for j in line])\n",
    "\n",
    "\n",
    "solve_sudoku()\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
